package com.cashmama.market.api.domain.utils;

import com.cashmama.market.api.infras.enums.PackageNameEnum;
import com.cashmama.market.api.domain.service.ApiUserService;
import com.fqgj.exception.common.ApplicationException;
import com.fqgj.xjd.user.client.response.UserAccessInfo;
import com.qianli.cashmama.product.facade.AppFacade;
import com.qianli.cashmama.product.facade.result.AppVo;
import com.qianli.cashmama.trade.common.enums.message.MessageBizCodeEnum;
import com.qianli.cashmama.trade.common.enums.message.MessageSceneEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 马甲包 消息工具类
 *
 * @author moss
 * 2019-04-02
 */
@Component
public class MessageUtils {

    @Autowired
    ApiUserService apiUserService;

    @Autowired
    private AppFacade appFacade;

    public AppVo getAppVo(String userCode) {
        try {
            UserAccessInfo userAccessInfo = apiUserService.getUserAccessInfoByUserCode(userCode);
            AppVo appVo = appFacade.getAppByAppCode(userAccessInfo.getAppCode()).getData();
            return appVo;
        } catch (Exception e) {
            return null;
        }
    }

    public AppVo getAppVoByAppCode(String appCode) {
        try {
            AppVo appVo = appFacade.getAppByAppCode(appCode).getData();
            return appVo;
        } catch (Exception e) {
            throw new ApplicationException("查询App信息失败");
        }
    }

    /**
     * 根据用户code 和 消息场景确定 消息bizCode
     */
    public String getMessageBizCode(String userCode, MessageSceneEnum messageSceneEnum) {
        return MessageBizCodeEnum.getMessageBizCodeEnum(this.getAppVo(userCode).getAppName(), messageSceneEnum);
    }

    /**
     * 根据用户code 和 消息场景确定 消息bizCode
     */
    public String getMessageBizCodeByPackageName(String userCode, MessageSceneEnum messageSceneEnum) {
        UserAccessInfo userAccessInfo = apiUserService.getUserAccessInfoByUserCode(userCode);
        //已经在运行的app 还是按照appName获取场景码
        if ("in.cashmama.app".equals(userAccessInfo.getPackageName()) || "in.dhanadhan.loan.money.cash".equals(userAccessInfo.getPackageName())
                || "35".equals(userAccessInfo.getAppCode()) || "36".equals(userAccessInfo.getAppCode())) {
            return MessageBizCodeEnum.getMessageBizCodeEnum(this.getAppVoByAppCode(userAccessInfo.getAppCode()).getAppName(), messageSceneEnum);
        } else {
            return MessageBizCodeEnum.getMessageBizCodeByPackageName(userAccessInfo.getPackageName(), messageSceneEnum);
        }
    }

    public String getMessageBizCodeByPackageName(String packageName, String appName, MessageSceneEnum messageSceneEnum) {
        //已经在运行的app 还是按照appName获取场景码
        if (PackageNameEnum.CASHMAMA.getPackageName().equals(packageName)
                || PackageNameEnum.DHANADHAN.getPackageName().equals(packageName)
                || PackageNameEnum.CREDFOX.getPackageName().equals(packageName)) {
            return MessageBizCodeEnum.getMessageBizCodeEnum(appName, messageSceneEnum);
        } else {
            return MessageBizCodeEnum.getMessageBizCodeByPackageName(packageName, messageSceneEnum);
        }
    }


}
